package servlets;

import excecoes.NaoLogadoException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import negocio.Conta;

@WebServlet(value="/saldo")
public class SaldoServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();

        Conta conta = (Conta) session.getAttribute("conta");
        if (conta == null) {
            throw new NaoLogadoException("Não existe conta-corrente conectada!");
        }

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("    <head>");
        out.println("        <title>Caixa Eletrônico</title>");
        out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        out.println("        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
        out.println("        <!-- Bootstrap -->");
        out.println("        <link href=\"css/bootstrap.css\" rel=\"stylesheet\">");
        out.println("        <!-- Custom styles for this template -->");
        out.println("        <link href=\"banco.css\" rel=\"stylesheet\">");
        
        out.println("        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->");
        out.println("        <!--[if lt IE 9]>");
        out.println("          <script src=\"../../assets/js/html5shiv.js\"></script>");
        out.println("          <script src=\"../../assets/js/respond.min.js\"></script>");
        out.println("        <![endif]-->");
        out.println("    </head>");
        out.println("    <body>");
        out.println("        <div class=\"container\">");

        out.println("          <div class=\"starter-template\">");
        
        out.println("            <h1>Saldo</h1>");
        out.printf("            <p class=\"lead\">Valor: R$ %.2f</p>\n", conta.getSaldo());
        out.println("            <p class=\"lead\"><a href=\"menu.html\">Menu Principal</a></p>");
        
        out.println("          </div>");

        out.println("        </div>");
    
        out.println("        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->");
        out.println("        <script src=\"//code.jquery.com/jquery.js\"></script>");
        out.println("        <!-- Include all compiled plugins (below), or include individual files as needed -->");
        out.println("        <script src=\"js/bootstrap.min.js\"></script>");
        out.println("    </body>");
        out.println("</html>");
        
        out.close();
    }
    
}